package org.itboys.core.service.mongo.front;


import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.core.dto.advertisment.SundayAdvertisementDto;
import org.itboys.core.entity.mongo.SundayAdvertisement;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告信息_service_移动端
 */
@Service
public class SundayFrontAdvertisementService extends BaseMongoService<SundayAdvertisement> {

    @Autowired
    private ResourceHolder resourceHolder;

    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayAdvertisement> getEntityClass() {
        return SundayAdvertisement.class;
    }


    /**
     * 根据广告位获取广告。不分页
     */
   public List<SundayAdvertisementDto> getAdvertisementByZone(String zoneKey){
       Map<String,Object> param = new HashMap<String,Object>();
       param.put("zoneKey",zoneKey);
       param.put("isDeleted",0);
       param.put("sort","sequence");
       param.put("order","desc");
        return pakageDtoMuilt(super.list(param));
   }

    /**
     * 批量广告位获取信息
     * @param zoneKeys
     * @return
     */
    public List<Map<String,Object>> getAdvertisementByZoneKeys(List<String> zoneKeys){
        List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
        if(ListUtil.isNull(zoneKeys))return results;
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("zoneKey in",zoneKeys);
        param.put("isDeleted",0);
        List<SundayAdvertisement> advertisements=super.list(param);
        //将zoneKey分组
        for(String zoneKey:zoneKeys){
            Map<String,Object> result =new HashMap<String,Object>();
            result.put("zoneKey",zoneKey);
            List<SundayAdvertisementDto> advs = new ArrayList<SundayAdvertisementDto>();
            for(SundayAdvertisement advertisement:advertisements){
                if(zoneKey.equalsIgnoreCase(advertisement.getZoneKey())){
                    advs.add(pakageDtoSingle(advertisement));
                }
            }
            result.put("advertisements",advs);
            results.add(result);
        }


        return results;
    }
    /**
     * 组装DTO
     * @param advertisement
     * @return
     */

    public SundayAdvertisementDto pakageDtoSingle(SundayAdvertisement advertisement){
       String url="/sunday/weixin/advertisement/html5?id="+advertisement.getId();
       String imgRoot=resourceHolder.getStringValue("imgRoot");
       String info="转跳信息说明。pageType为0，打开Url；pageType为1，打开商品详情，pageId为商品ID；pageType=2,打开商品列表，pageId为商品分类ID；pageType=3,打开商户详情（暂无），pageId为商户ID"+
               "广告位用于银行图标获取时name：银行名称，viceName：银行类型";
        return new SundayAdvertisementDto(
                advertisement.getId(),advertisement.getCreateTime(),advertisement.getCreateDate(),
                advertisement.getName(),advertisement.getViceName(),imgRoot+advertisement.getImage(), advertisement.getPageType(),
                advertisement.getPageId(),url,info,advertisement.getDesc());

    }

    public  List<SundayAdvertisementDto>  pakageDtoMuilt(List<SundayAdvertisement> advertisements){
        List<SundayAdvertisementDto> dtos = new ArrayList<SundayAdvertisementDto>();
        for(SundayAdvertisement advertisement:advertisements){
            dtos.add(pakageDtoSingle(advertisement));
        }
        return dtos;

    }

}
